package com.itic.system.xhproduct.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.HttpRequestUtils;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.system.xhcategory.persistence.model.XhCategory;
import com.itic.system.xhcategory.persistence.model.XhCategoryExample;
import com.itic.system.xhcategory.service.XhCategoryService;
import com.itic.system.xhproduct.persistence.model.XhProduct;
import com.itic.system.xhproduct.persistence.model.XhProductExample;
import com.itic.system.xhproduct.persistence.model.XhProductExample.Criteria;
import com.itic.system.xhproduct.service.XhProductService;


@Controller
@RequestMapping(value="/system/xhproduct")
public class XhProductController
     extends BaseController {
    @Autowired
    private XhProductService xhProductService;

    @Autowired
    private XhCategoryService xhCategoryService;
    
    @Autowired
   	protected OutCommonService outCommonService;
       
       //////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
       private static final String MODEL_NAME_CHINA = "商品管理";//中文名
       private static final String MODEL_NAME＿ENGLISH = "xhproduct";//英文名
       private static final String PAGE_COMMON_PATH = "/xianhuo/system/xhproduct/xhproduct";//通用路径
       private static final String PARENT_COMMON_URL = "/system/xhcategory/";//通用路径
       private static final String COMMON_URL = "/system/xhproduct/";//通用路径
       
       //////////////////////////////////////////////////////页面跳转区////////////////////////////////////////////////////////////
      
       /*
        * 主页面
        */
       @RequestMapping(value="/init")
       public String init(Model model){
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Main";
       }
       
       /*
        * 主页面
        */
       @RequestMapping(value="/initList")
       public String initList(String categoryId,Model model){
       	model.addAttribute("categoryId", categoryId);
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"List";
       }
       
       /*
        * 添加页面
        */
       @RequestMapping(value="/initAdd")
       public String initAdd(String categoryId,Model model){
       	if( StringHelper.isNotBlank(categoryId) ){
       		model.addAttribute("categoryId", categoryId);
       	}
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Add";
       }
       
       /*
        * 编辑页面
        */
       @RequestMapping(value="/initEdit")
       public String initEdit(String id , String categoryId, Model model){
       	if( StringHelper.isNotBlank(id) ) {
       		XhProduct temp = xhProductService.get(id);
       		model.addAttribute("editData",iticJsonMapper.toJson(temp));
       		model.addAttribute("name_china", MODEL_NAME_CHINA);
           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
           	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
           	model.addAttribute("common_url", COMMON_URL);
           	model.addAttribute("categoryId", categoryId);
       	}else {
               throw new ParameterErrorException();
           }
       	return PAGE_COMMON_PATH+"Edit";
       }
       
       
       /*
        * 查看页面
        */
       @RequestMapping(value="/initView")
       public String initView(String id , String categoryId, Model model){
       	if( StringHelper.isNotBlank(id) ) {
       		
       		XhProduct temp  = xhProductService.get(id);
    		
       		model.addAttribute("product",iticJsonMapper.toJson(temp));
       		
       		model.addAttribute("name_china", MODEL_NAME_CHINA);
           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
           	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
           	model.addAttribute("common_url", COMMON_URL);
           	model.addAttribute("categoryId", categoryId);
           	
       	}else {
               throw new ParameterErrorException();
           }
       	
       	return PAGE_COMMON_PATH+"View";
       }
       
       /*
        * 导入页面
        */
       @RequestMapping(value="/initImp")
       public String initImp(Model model){
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Imp";
       }
       
       
       //////////////////////////////////////////////////////业务代码区////////////////////////////////////////////////////////////
       
       /**
        * 添加模板管理数据
        * @param android
        * @return
        */
       @RequestMapping(value="/add")
       @ResponseBody
       public Map<String,String> add( XhProduct product){
       	
       	    Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            /*if( StringHelper.isNotBlank(product.getVedeo()) ){
            	String url = "http://develop.jhjvip.cn:8081/ddt/mobile/ffmpeg/getVideoCoverPic?videoUrl="+product.getVedeo();
        		String httpGet = HttpRequestUtils.httpGet(url);
        		product.setVideoImage(httpGet);
            }*/
            result = xhProductService.add(product);
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
            
       }
       
       
       /**
        * <p>Title:queryCondition </p>
        * <p>Description:封装查询条件 </p>
        * @param jzdw
        * @param jzdwExample
        * @return
        */
       private XhProductExample queryCondition(HttpServletRequest request,XhProduct temp) {
       	
       	   // 参数处理
           XhProductExample example = new XhProductExample();
           // 按创建时间倒序
           example.setOrderByClause("UPDATE_TIME DESC");
           // 按排列号顺序
           //example.setOrderByClause("reorder asc");
           
           Criteria criteria = example.createCriteria().andIsEffectiveEqualTo( DBConst.TRUE );
           
           if( StringHelper.isNotBlank(temp.getCategoryId()) ){
        	   List<String> ids = new ArrayList<String>();
        	   // XhCategory category = xhCategoryService.get(temp.getCategoryId());
        	   // ids.add(category.getParentId());
        	   ids.add(temp.getCategoryId());
        	   //添加下级
        	   ids.addAll(getSubCategoryIds(temp.getCategoryId()));
        	   //过虑类别条件
               criteria.andCategoryIdIn(ids);
           }
           
           // 查询条件
           if ( null != temp ) {
       	       //模糊查询
               if ( StringHelper.isNotBlank( temp.getName() ) ) {
                   criteria.andNameLike("%" + temp.getName() + "%");
               }
               
           }
           return example;
       }
       
       private List<String> getSubCategoryIds(String categoryId) {
    	    List<String> result = new ArrayList<String>();
			XhCategoryExample example = new XhCategoryExample();
			example.createCriteria().andParentIdEqualTo(categoryId);
			List<XhCategory> list = xhCategoryService.list(example);
			if( list.size() > 0 ){
				for( XhCategory temp: list ){
					result.add(temp.getId());
				}
			}
		    return result;
	  }

	/**
        * 
        * @param goldType
        * @param request
        * @return
        */
       @RequestMapping(value = "/list")
       @ResponseBody
       public Map<String, Object> list(XhProduct temp, HttpServletRequest request){
           // 声明返回对象
           Map<String, Object> resultMap = new HashMap<String, Object>();
           
           
           XhProductExample example = this.queryCondition(request,temp);
           // 封装分页参数
           example = this.handlePageQuery(request, example);
           
           
           // 获取信息总条数
           int total = xhProductService.count(example);
           
           // 获取数据
           List<XhProduct> list = xhProductService.list(example);
           
           // 数据返回处理
           resultMap.put(IticConst.TOTAL_RECORDS, total);
           resultMap.put(IticConst.DISPLAY_RECORDS, total);
           resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
           resultMap.put(IticConst.DATA, list);
           return resultMap;

       }
       
       /**
        * 编辑保存
        * @param Category
        * @return
        */
       @RequestMapping(value="/edit")
       @ResponseBody
       public Map<String,String> edit(XhProduct temp){
       
       	    Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            /*if( StringHelper.isNotBlank(temp.getVedeo()) ){
            	String url = "http://develop.jhjvip.cn:8081/ddt/mobile/ffmpeg/getVideoCoverPic?videoUrl="+temp.getVedeo();
        		String httpGet = HttpRequestUtils.httpGet(url);
        		temp.setVideoImage(httpGet);
            }*/
            result = xhProductService.edit(temp);
            
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
       }
       
       

       /**
        * 删除
        * 
        */
       @RequestMapping(value = "/del")
       @ResponseBody
       public Map<String, String> del(String id) {
           Map<String, String> resultMap = new HashMap<String, String>();
           String result = ResponseCode.SERVER_ERROR.getCode();

           //当前商品的数量减一
           XhProduct product = xhProductService.get(id);
           if( null != product ){
        	  result = xhProductService.delete(product);
           }
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       }
       
       
       /**
        * 编辑保存
        * @param Category
        * @return
        */
       @RequestMapping(value="/addnew")
       @ResponseBody
       public Map<String,String> addnew(String id){
       	    Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            XhProduct product = new XhProduct();
            product.setId(id);
            product.setIsNew(DBConst.TRUE);
        	result = xhProductService.edit(product);
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
       }
       
       
       /**
        * 编辑保存
        * @param Category
        * @return
        */
       @RequestMapping(value="/delnew")
       @ResponseBody
       public Map<String,String> delnew(String id){
       	    Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            XhProduct product = new XhProduct();
            product.setId(id);
            product.setIsNew(DBConst.FALSE);
        	result = xhProductService.edit(product);
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
       }
       
       /**
        * 编辑保存
        * @param Category
        * @return
        */
       @RequestMapping(value="/addadvice")
       @ResponseBody
       public Map<String,String> addadvice(String id){
       	    Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            XhProduct product = new XhProduct();
            product.setId(id);
            product.setIsAdvice(DBConst.TRUE);
        	result = xhProductService.edit(product);
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
       }
       
       
       /**
        * 编辑保存
        * @param Category
        * @return
        */
       @RequestMapping(value="/deladvice")
       @ResponseBody
       public Map<String,String> deladvice(String id){
       	    Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            XhProduct product = new XhProduct();
            product.setId(id);
            product.setIsAdvice(DBConst.FALSE);
        	result = xhProductService.edit(product);
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
       }
       
       
}